<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/11/25 0025
 * Time: 17:57
 */
class Payment {

    /**
     * 生成微信支付数据包
     * @param $orderId
     * @param $deviceType
     * @return array
     */
    public static function createWxpayPackage($orderId, $deviceType) {

        global $config;

        $helper = new PayHelperWxpay();

        // 订单总额
        $totalFee = Order::getOrderTotalFee($orderId) * 100;

        // 订单信息
        $orderInfo = Order::findFirst("order_id = $orderId");

        $nonceStr = $helper->createNoncestr();

        $timeStamp = strval(time());

        $pack = array(
            'appid' => $config->wxpay->appid,
            'body' => $config->base->shopname,
            'timeStamp' => $timeStamp,
            'mch_id' => $config->wxpay->partner,
            'nonce_str' => $nonceStr,
            'notify_url' => $config->base->payment_notify_wxpay,
            'out_trade_no' => $orderInfo->serial_number,
            'spbill_create_ip' => Util::getIp(),
            'total_fee' => $totalFee,
            'trade_type' => 'APP'
        );

        $pack['sign'] = $helper->paySign($pack, $config->wxpay->partner_key);

        $xml = $helper->toXML($pack);

        $ret = Curl::post('https://api.mch.weixin.qq.com/pay/unifiedorder', $xml);

        $postObj = json_decode(json_encode(simplexml_load_string($ret, 'SimpleXMLElement', LIBXML_NOCDATA)));

        if (empty($postObj->prepay_id) || $postObj->return_code == "FAIL") {
            // 支付发起错误 记录到logs
            Util::log('wepay_error:' . $postObj->return_msg . PHP_EOL . $xml);
        }

        if (!isset($postObj->prepay_id)) {
            $postObj->prepay_id = 'asdasdasdasdasdasd';
        }

        $packApp = array(
            'appId' => $config->wxpay->appid,
            'partnerId' => $config->wxpay->partner,
            'prepayId' => $postObj->prepay_id,
            'timeStamp' => $timeStamp,
            'nonceStr' => $nonceStr,
            'package' => "Sign=WXPay",
        );

        if ($deviceType == 1) {
            $packApp['appId'] = $config->wxpay->appid;
            $packApp['packageValue'] = "Sign=WXPay";
            unset($packApp['package']);
        }

        $JsSign = $helper->paySign($packApp, $config->wxpay->partner_key);

        $packApp['sign'] = $JsSign;

        return $packApp;

    }

    /**
     * 生成支付宝支付数据包
     * @param $orderId
     * @param $deviceType
     */
    public static function createAlipayPackage($orderId) {

        global $config;

        $helper = new PayHelperAlipay();

        // 订单总额
        $totalFee = Order::getOrderTotalFee($orderId);

        // 订单信息
        $orderInfo = Order::findFirst("order_id = $orderId");

        $ali = array(
            'service' => 'mobile.securitypay.pay',
            'partner' => $config->alipay->partner,
            '_input_charset' => 'utf-8',
            'sign_type' => 'MD5',
            'sign' => '',
            'notify_url' => urlencode($config->base->payment_notify_alipay),//回调地址
            'out_trade_no' => $orderInfo->serial_number,//商户网站唯一订单号
            'subject' => $config->base->shopname,//商品名称
            'payment_type' => 1,//支付类型
            'seller_id' => $config->alipay->partner,//支付宝账号
            'total_fee' => $totalFee,//总金额
            'body' => $config->base->shopname//商品详情
        );

        $ali = $helper->argSort($ali);
        $str = '';

        foreach ($ali as $key => $val) {
            if ($key == 'sign_type' || $key == 'sign') {
                continue;
            } else {
                if ($str == '') {
                    $str = $key . '=' . '"' . $val . '"';
                } else {
                    $str = $str . '&' . $key . '=' . '"' . $val . '"';
                }
            }
        }

        $sign = $helper->md5sign($str, $config->alipay->partner_key);

        #$sign = urlencode($helper->sign($str));
        $str = $str . '&sign=' . '"' . $sign . '"' . '&sign_type=' . '"' . $ali['sign_type'] . '"';//传给支付宝接口的数据

        return str_replace('"', '\"', $str);

    }

}